//
//  ItemPickerViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 28..
//  Copyright 2011 Daum. All rights reserved.
//

#import "ItemPickerViewController.h"


@implementation ItemPickerViewController
@synthesize itemPickerDataSource = _itemPickerDataSource;
@synthesize itemPickerView = _itemPickerView;
@synthesize showLabel = _showLabel;
@synthesize applyBtn = _applyBtn;
@synthesize cancelBtn = _cancelBtn;
/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.itemPickerDataSource = [[ItemPickerDataSource alloc] init];
        [_itemPickerDataSource setIsIn:@"out"];
    }
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    [_applyBtn release];
    [_cancelBtn release];
    [_showLabel release];
    [_itemPickerDataSource release];
    [_itemPickerView release];
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"뷴류 선택";
    self.itemPickerView.dataSource = _itemPickerDataSource;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload{
    self.applyBtn=nil;
    self.cancelBtn=nil;
    self.showLabel=nil;
    self.itemPickerDataSource = nil;
    self.itemPickerView = nil;
    [super viewDidUnload];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_itemPickerDataSource loadItemGroupByIsIn:_itemPickerDataSource.isIn];
    [_itemPickerDataSource loadItemByItemGroupId:[[_itemPickerDataSource.itemGroupPickerArray objectAtIndex:_itemGroupSelectedIndex] objectForKey:@"item_group_id"]];
    [_itemPickerView reloadComponent:1];
    [_itemPickerView reloadComponent:2];                    
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -
#pragma mark UIPickerViewDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    NSLog(@"titleForRow");
	NSString *returnStr = @"";
    switch (component) {
        case 0:
            returnStr = [_itemPickerDataSource.isInArray objectAtIndex:row];
            break;
        case 1:
            returnStr = [[_itemPickerDataSource.itemGroupPickerArray objectAtIndex:row] objectForKey:@"title"];            
            break;            
        case 2:
            returnStr = [[_itemPickerDataSource.itemPickerArray objectAtIndex:row] objectForKey:@"title"];            
            break;            
    }
	return returnStr;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
//    NSLog(@"widthForComponent");
	CGFloat componentWidth = 0.0;    
    switch (component) {
        case 0:
            componentWidth = 60.0;
            break;
        case 1:
            componentWidth = 110.0;
            break;
        case 2:
            componentWidth = 110.0;
            break;            
        default:
            break;
    }
	return componentWidth;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
//    NSLog(@"rowHeightForComponent");
	return 40.0;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
	if (pickerView == _itemPickerView){
        switch (component) {
            case 0:
                if(row==0){
                    [_itemPickerDataSource loadItemGroupByIsIn:@"out"];
                }else{
                    [_itemPickerDataSource loadItemGroupByIsIn:@"in"];
                }                
                [_itemPickerDataSource loadItemByItemGroupId:[[_itemPickerDataSource.itemGroupPickerArray objectAtIndex:0] objectForKey:@"item_group_id"]];
                [_itemPickerView reloadComponent:1];
                [_itemPickerView reloadComponent:2];                
                break;
            case 1:
                _itemGroupSelectedIndex = row;
                _itemPickerDataSource.itemGroupName = [[_itemPickerDataSource.itemGroupPickerArray objectAtIndex:row] objectForKey:@"title"];
                _itemPickerDataSource.itemGroupId = [[_itemPickerDataSource.itemGroupPickerArray objectAtIndex:row] objectForKey:@"item_gruop_id"];
                [_itemPickerDataSource loadItemByItemGroupId:[[_itemPickerDataSource.itemGroupPickerArray objectAtIndex:row] objectForKey:@"item_group_id"]];
                [_itemPickerView reloadComponent:2];                
                break;
            case 2:
                if([_itemPickerDataSource.itemPickerArray count]>row){
                    _itemPickerDataSource.itemName = [[_itemPickerDataSource.itemPickerArray objectAtIndex:row] objectForKey:@"title"];                
                    _itemPickerDataSource.itemId = [[_itemPickerDataSource.itemPickerArray objectAtIndex:row] objectForKey:@"item_id"];
                }
                break;
                
            default:
                break;
        }
        NSString* title = [NSString stringWithFormat:@"%@ > %@", _itemPickerDataSource.itemGroupName, _itemPickerDataSource.itemName];            
        [_showLabel setText:title];
	}
}
#pragma mark -
#pragma mark UIButtonDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (IBAction)openItemManage:(id)sender {
    ItemGroupSettingViewController *viewController = [[[ItemGroupSettingViewController alloc] initWithNibName:@"ItemGroupSetting" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];                    
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(IBAction)applyAct:(id)sender{
    NSLog(@"applyAct");
    if([_itemPickerDataSource.itemId intValue]==-1){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"웁스" message:@"마지막 분류를 선택해 주세요." delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil];
        [alert show];	
        [alert release];

    }else{
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
      
        [dic setObject:_itemPickerDataSource.itemGroupId forKey:@"itemGroupId"];
        [dic setObject:_itemPickerDataSource.itemGroupName forKey:@"itemGroupName"];
        [dic setObject:_itemPickerDataSource.itemId forKey:@"itemId"];
        [dic setObject:_itemPickerDataSource.itemName forKey:@"itemName"];        
        [dic setObject:[NSString stringWithFormat:@"%@ > %@", _itemPickerDataSource.itemGroupName, _itemPickerDataSource.itemName] forKey:@"showName"];        

        [_resultDelegate applyResultSet:self resultDic:dic];
        
        [[self navigationController] popViewControllerAnimated:YES];
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(IBAction)cancelAct:(id)sender{
    NSLog(@"cancelAct");
    [[self navigationController] popViewControllerAnimated:YES];
}

@end
